﻿//反填修改原料
var image = "";

layui.use(['table', 'dropdown', 'layer', 'form'], function () {
    var upload = layui.upload;
 
    var element = layui.element;
    var $ = layui.$;
    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    $(function () {
        Fan();
    });
    var apiUrl = $('#apiUrl').data('url');
    var uploadInst = upload.render({
        elem: '#ID-upload-demo-btn',
        url: apiUrl + 'yuan/api/Ingredient/Upfile', // 实际使用时改成您自己的上传接口即可。
        before: function (obj) {
          
            // 预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
               
                $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
            });

            element.progress('filter-demo', '0%'); // 进度条复位
            layer.msg('上传中', { icon: 16, time: 0 });
        },
        done: function (res) {
            image = res.data
          
            // 若上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            // 上传成功的一些操作
            // …
            $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
        },
        error: function () {
            // 演示失败状态，并实现重传
            var demoText = $('#ID-upload-demo-text');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        },
        // 进度条
        progress: function (n, elem, e) {
            element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
            if (n == 100) {
                layer.msg('上传完毕', { icon: 1 });
            }
        }
    });
   
    window.Fan = function Fan() {
        var ingredientId = localStorage.getItem("ingredientId")
        console.log(ingredientId)
        $.ajax({
        url: '../Ingredient/IngredientFanDo',
            data: {
                IngredientId: ingredientId,
                token: ""
            },
            type: 'Get',
            success: function (res) {
                var res = JSON.parse(res)
               
        $.each(res, function () {
                  
                    $("#Ingredientnumber").val(this.ingredientnumber),
                        $("#IngredientName").val(this.ingredientName),
                        $("#IngredientFen").val(this.ingredientFen),
                        form.render('select'); // 重新渲染下拉框
                    $("#size").val(this.size),
                        form.render('select'); // 重新渲染下拉框
                    $("#specification").val(this.specification),
                        form.render('select'); // 重新渲染下拉框
                    $("#Rawmaterialproperties").val(this.rawmaterialproperties),
                        form.render('select'); // 重新渲染下拉框
                    $("#unit").val(this.unit),
                        form.render('select'); // 重新渲染下拉框
                    $("#Num").val(this.num),
                        form.render('select'); // 重新渲染下拉框
                    $("#IngredientType").val(this.ingredientType),
                        form.render('select'); // 重新渲染下拉框
                        $("#State").val(this.state),
                        $("#IngredientNum").val(this.ingredientNum),
                        $("#alarmDays").val(this.alarmDays),
                        $("#InventoryLimit").val(this.inventoryLimit),
                        $("#InventoryMinimum").val(this.inventoryMinimum),
                            $("#Rawmateriallocation").val(this.rawmateriallocation),
                        $("#CreatedTimess").val(this.createdTimess),
                            $("#remark").val(this.remark),
                         $("#ID-upload-demo-img").attr("src",apiUrl+"yuan/Pic/"+ this.images),
                              $("#PickingId").val(this.pickingId),
                        $("#PickingNum").val(this.pickingNum)

                })
                }
        })
    }


    
    window.DeviceUpdate = function DeviceUpdate() {
        IngredientId = localStorage.getItem("ingredientId")
        Ingredientnumber = $("#Ingredientnumber").val(),
            IngredientName = $("#IngredientName").val(),
            IngredientFen = $("#IngredientFen").val(),
            size = $("#size").val(),
            specification = $("#specification").val(),
            Rawmaterialproperties = $("#Rawmaterialproperties").val(),
            unit = $("#unit").val(),
            Num = $("#Num").val(),
            IngredientType = $("#IngredientType").val(),
            State = $("[name='State']:checked").val(),
            IngredientNum = $("#IngredientNum").val(),
            alarmDays = $("#alarmDays").val(),
            InventoryLimit = $("#InventoryLimit").val(),
            InventoryMinimum = $("#InventoryMinimum").val(),
            Rawmateriallocation = $("#Rawmateriallocation").val(),
            CreatedTimess = $("#CreatedTimess").val(),
            remark = $("#remark").val(),
            console.log(CreatedTimess)
        images = image,
            PickingId = "",
            PickingNum = "",
            token=""
       
        var token = localStorage.getItem("token")
        $.ajax({
            url: '../Ingredient/IngredientUodate',
            type: 'post',
            data: {
                IngredientId: IngredientId,

                Ingredientnumber: Ingredientnumber,
                IngredientName: IngredientName,
                IngredientFen: IngredientFen,
                size: size,
                specification: specification,
                Rawmaterialproperties: Rawmaterialproperties,
                unit: unit,
                Num: Num,
                IngredientType: IngredientType,
                State: State ,
                IngredientNum: IngredientNum ,
                alarmDays: alarmDays ,
                InventoryLimit: InventoryLimit ,
                InventoryMinimum: InventoryMinimum,
                Rawmateriallocation: Rawmateriallocation ,
                CreatedTimess: CreatedTimess,
                remark: remark ,
                images: images,
                PickingId: PickingId,
                PickingNum: PickingNum,
                token:token
 },
            //contentType: 'application/json',
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                if (res.data > 0) {
                    alert("修改成功")
                    window.location.href = '/Ingredient/ShowIngredient'
                }
                else {
                    alert("修改失败")
                }

            }
        })
    }
});